package com.xxx.video.login.presenters;

import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.xxx.video.R;
import com.xxx.video.commen.configs.UserManager;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.login.beans.RegisterBundleInfoBean;
import com.xxx.video.login.beans.RegisterSecondQuestionBean;
import com.xxx.video.login.beans.RegisterSuccessBean;
import com.xxx.video.login.beans.SecurityOptionsBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

import java.util.ArrayList;
import java.util.List;

public class RegisterSecondPresenter extends BasePresenter<IRegisterSecondView> {

    public RegisterSecondPresenter(@NonNull Context context, IRegisterSecondView baseView) {
        super(context, baseView);
    }

    /**
     * get security options
     *
     * @param number question numer
     */
    public void getSecurityOptions(int number) {
        showProgressDialog("loading");
        NetWorkUtils.getInstance().getSecurityOptions(new CommonResponseListener<SecurityOptionsBean>() {
            @Override
            public void onSuccess(SecurityOptionsBean successOb, String result) {
                hideProgressDialog();
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().initSecurityData2View(successOb, number);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
            }
        });

    }

    /**
     * register
     *
     * @param registerBundleInfoBean register info
     */
    public void register(RegisterBundleInfoBean registerBundleInfoBean, String longitude, String latitude) {
        showProgressDialog("loading");
        SecurityOptionsBean.ListBean[] questionList = getBaseView() == null ? null : getBaseView().getQuestionList();
        String firstAnswer = getBaseView() == null ? "" : getBaseView().getFirstAnswer();
        String secondAnswer = getBaseView() == null ? "" : getBaseView().getSecondAnswer();
        String thirdAnswer = getBaseView() == null ? "" : getBaseView().getThirdAnswer();
        boolean isAgreeAgency = getBaseView() != null && getBaseView().isAgreeUserAgency();
        if (!isAgreeAgency) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_register_second_please_agree_user_agency));
            hideProgressDialog();
            return;
        }
        if (questionList == null) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_register_second_check_security_question_can_not_empty));
            hideProgressDialog();
            return;
        }
        if (questionList[0] == null && questionList[1] == null && questionList[2] == null) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_register_second_check_security_question_can_not_empty));
            hideProgressDialog();
            return;
        }
        if (TextUtils.isEmpty(firstAnswer) && TextUtils.isEmpty(secondAnswer) && TextUtils.isEmpty(thirdAnswer)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_register_second_check_security_answer_can_not_empty));
            hideProgressDialog();
            return;
        }
        if (questionList[0] != null) {
            if (TextUtils.isEmpty(firstAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_register_second_check_security_first_answer_can_not_empty));
                hideProgressDialog();
                return;
            }
        }
        if (questionList[1] != null) {
            if (TextUtils.isEmpty(secondAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_register_second_check_security_second_answer_can_not_empty));
                hideProgressDialog();
                return;
            }
        }
        if (questionList[2] != null) {
            if (TextUtils.isEmpty(thirdAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_register_second_check_security_third_answer_can_not_empty));
                hideProgressDialog();
                return;
            }
        }

        List<RegisterSecondQuestionBean> registerSecondQuestionBeanList = new ArrayList<>();
        if (questionList[0] != null) {
            RegisterSecondQuestionBean registerSecondQuestionBean = new RegisterSecondQuestionBean.Builder()
                    .fQuestionId(questionList[0].fDatadictId)
                    .fQuestionName(questionList[0].fDatadictDesc)
                    .fAnswer(firstAnswer)
                    .build();
            registerSecondQuestionBeanList.add(registerSecondQuestionBean);
        }
        if (questionList[1] != null) {
            RegisterSecondQuestionBean registerSecondQuestionBean1 = new RegisterSecondQuestionBean.Builder()
                    .fQuestionId(questionList[1].fDatadictId)
                    .fQuestionName(questionList[1].fDatadictDesc)
                    .fAnswer(secondAnswer)
                    .build();
            registerSecondQuestionBeanList.add(registerSecondQuestionBean1);
        }
        if (questionList[2] != null) {
            RegisterSecondQuestionBean registerSecondQuestionBean2 = new RegisterSecondQuestionBean.Builder()
                    .fQuestionId(questionList[2].fDatadictId)
                    .fQuestionName(questionList[2].fDatadictDesc)
                    .fAnswer(thirdAnswer)
                    .build();
            registerSecondQuestionBeanList.add(registerSecondQuestionBean2);
        }
        for (int i = 0; i < registerSecondQuestionBeanList.size(); i++) {
            registerSecondQuestionBeanList.get(i).fSort = i + 1;
        }
        String androidId = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
        NetWorkUtils.getInstance().register(registerBundleInfoBean.userName, registerBundleInfoBean.pwd,
                registerBundleInfoBean.phone, registerBundleInfoBean.email, registerBundleInfoBean.nickName, registerSecondQuestionBeanList, androidId,
                longitude, latitude, new CommonResponseListener<RegisterSuccessBean>() {
                    @Override
                    public void onSuccess(RegisterSuccessBean successOb, String result) {
                        hideProgressDialog();
                        if (ResponseUtils.getInstance().isNoError(successOb)) {
                            if (getBaseView() != null) {
                                getBaseView().registerSuccess(successOb);
                                UserManager.getInstance().putSaveSecurityOptionsBean("");
                            }
                        }
                    }

                    @Override
                    public void onFailure(Object failureObj) {
                        hideProgressDialog();
                    }
                });
    }
}
